import axios from "axios";
import base from "@/api/base.js";
import blogStore from "@/stores/blog.js";

/*
创建对象
 */
const req = axios.create({
    baseURL: base.baseurl,
    timeout: 5000
})

/*
连接器 发送和接受之前

 */

req.interceptors.request.use(
    config=>{
        const token=blogStore().token

        config.headers['Authorization']=token
        return config
    }
)






//所有post请求添加成json头
req.interceptors.request.use(
    config => {
        if (config.method === "post") {
            config.headers['Content-Type'] = 'application/json'
        }
        return config
    },
    error => Promise.reject(error)
)
/*


req.interceptors.response.use(
    response => response.status === 200 ? Promise.resolve(Promise) : Promise.reject(response),
    error => {
        const {response} = error;
        if (response) {
            errorHandler(response.status, response.info)
        } else {
            console.log("断网了")
        }
    }
)

function errorHandler(status, info) {
    switch (status) {
        case 400:
            console.log("语法错误")
            break
        case 500:
            console.log("后端异常")
            break
        default:
            console.log(info)
            break
    }
}
 */

export default req